package com.fadadianqi.logistics.search;

import com.fadadianqi.logistics.customer.model.Village;
import com.fadadianqi.logistics.customer.service.CustomerService;
import com.fadadianqi.search.AbstractSearcher;
import com.fadadianqi.search.Argument;
import com.fadadianqi.search.IndexPathHelper;
import com.fadadianqi.search.IndexPathHelper.IndexEnum;
import com.yeqiangwei.commons.util.BeanUtils;
import com.yeqiangwei.commons.util.Pagination;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.highlight.Highlighter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class VillageSearcher extends AbstractSearcher {

    private IndexPathHelper indexPathHelper = new IndexPathHelper(IndexEnum.VILLAGE);

    @Override
    protected Argument initializtionArgument(String keyword,
                                             Pagination pagination) {
        Argument argument = Argument.getInstance();
        argument.setPagination(pagination);
        argument.setArgs(Village.PROP_NAME, keyword, Occur.SHOULD, 0.9F);
        argument.setArgs(Village.PROP_INITIAL, keyword, Occur.SHOULD, 0.9F);
        argument.setArgs(Village.PROP_PINYIN, keyword, Occur.SHOULD, 0.9F);
        return argument;
    }

    @Override
    protected Object documentToVO(IndexSearcher searcher, Document doc,
                                  Highlighter highlighter) throws IOException {
        VillageVO vo = new VillageVO();
        Village village = customerService.getVillageById(doc.get(Village.PROP_ID));
        BeanUtils.copyProperties(vo, village);
        return vo;
    }

    @Override
    protected String getIndexPath() {
        return indexPathHelper.getIndexPath();
    }

    @Autowired
    private CustomerService customerService;

    public static class VillageVO extends Village {

        /**
         *
         */
        private static final long serialVersionUID = -2251648399865877320L;

    }

}
